package com.dlwx.parttimetalent.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.BaseBean;
import com.dlwx.parttimetalent.bean.TuiGuangBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.DialogUtils;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WithDrawActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.title_right_one_tv)
    TextView titleRightOneTv;
    @BindView(R.id.ll_account)
    LinearLayout llAccount;
    @BindView(R.id.et_money)
    EditText etMoney;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.tv_account)
    TextView tvAccount;
    @BindView(R.id.et_psw)
    EditText etPsw;
    @BindView(R.id.tv_explain)
    TextView tvExplain;
    private int accounttype;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_with_draw);
        ButterKnife.bind(this);
        initView();
        getExpalin();
    }

    private void initView() {
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setVisibility(View.VISIBLE);
        titleRightOneTv.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("提现");
        titleRightOneTv.setText("账户设置");
    }

    @OnClick({R.id.title_left_one_btn, R.id.title_right_one_tv, R.id.ll_account, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.title_right_one_tv:
                startActivity(new Intent(this, SetAccountActivity.class));
                break;
            case R.id.ll_account:
                startActivityForResult(new Intent(this, AccountActivity.class), 0);
                break;
            case R.id.tv_confirm:
                confirm();
                break;
        }
    }

    private void confirm() {
        String money = etMoney.getText().toString();
        if (TextUtils.isEmpty(money)) {
            ToastUtils.toastForShort(this, "金额不能为空");
            return;
        }
        String psw = etPsw.getText().toString();
        if (TextUtils.isEmpty(psw)) {
            ToastUtils.toastForShort(this, "密码不能为空");
            return;
        }
        loadingDialog.show();
        OkgoUtil.post(Http.WithDraw, WithDrawActivity.this)
                .params("accounttype", accounttype)
                .params("price", money)
                .params("paypwd", psw)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        BaseBean parentBean = DealResult.getInstance().dealDatas(WithDrawActivity.this, response, new TypeToken<BaseBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        int code = parentBean.getCode();
                        if (code == Constant.SUCESS) {
                            tvAccount.setText("");
                            etMoney.setText("");
                            etPsw.setText("");
                        } else {
                        }
                        DialogUtils.getMaterialDialogOneBtn(WithDrawActivity.this,parentBean.getResult()).show();
//                        ToastUtils.toastForShort(WithDrawActivity.this, parentBean.getResult());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        loadingDialog.dismiss();
                        super.onError(response);
                        ToastUtils.toastForShort(WithDrawActivity.this, getString(R.string.bad_net));
                    }
                });
    }

    private void getExpalin() {
        OkgoUtil.post(Http.WithDrawExplain, WithDrawActivity.this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        TuiGuangBean parentBean = DealResult.getInstance().dealDatas(WithDrawActivity.this
                                , response, new TypeToken<TuiGuangBean>() {
                                }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        int code = parentBean.getCode();
                        if (code == Constant.SUCESS) {
                            String str=parentBean.getBody()==null?"":parentBean.getBody().getTextcontent();
                            tvExplain.setText(str);
                        } else {

                            ToastUtils.toastForShort(WithDrawActivity.this, parentBean.getResult());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        loadingDialog.dismiss();
                        super.onError(response);
                        ToastUtils.toastForShort(WithDrawActivity.this, getString(R.string.bad_net));
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case Constant.ACCOUNT_BANK:
                tvAccount.setText("银行");
                accounttype = Constant.ACCOUNT_BANK;
                break;
            case Constant.ACCOUNT_Ali:
                tvAccount.setText("支付宝");
                accounttype = Constant.ACCOUNT_Ali;
                break;
            case Constant.ACCOUNT_WEIXIN:
                accounttype = Constant.ACCOUNT_WEIXIN;
                tvAccount.setText("微信");

                break;
        }
    }
}
